/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-08
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-26
 * @Description: 
 */
import 'package:chat_client/utils/date_formater.dart';
import 'package:hive/hive.dart';

part 'user_model.g.dart';

@HiveType(typeId: 1)
class User {
  @HiveField(0)
  final String userId;

  // 电话号
  @HiveField(1)
  final String phoneNumber;

  // 用户名
  @HiveField(2)
  final String username;

  // 昵称
  @HiveField(3)
  final String nickname;

  // 头像
  @HiveField(4)
  final String avatarUrl;

  // 性别
  @HiveField(5)
  final int gender;

  // 生日
  @HiveField(6)
  final int birthday;

  // 地区
  @HiveField(7)
  final String region;

  // 地区号
  @HiveField(8)
  final String regionCode;

  // 个性签名
  @HiveField(9)
  final String signature;

  // 加好友验证方式
  @HiveField(12)
  final int addType;

  // 加好友验证方式
  @HiveField(13)
  final int createAt;

  User({
    required this.userId,
    required this.phoneNumber,
    required this.username,
    required this.nickname,
    required this.avatarUrl,
    required this.gender,
    required this.birthday,
    required this.region,
    required this.regionCode,
    required this.signature,
    required this.addType,
    required this.createAt,
  });

  /// 从 JSON 构造 `User` 对象
  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      userId: json['userId'] ?? json['_id'] ?? '',
      phoneNumber: json['phoneNumber'] ?? '',
      username: json['username'] ?? '',
      nickname: json['nickname'] ?? '',
      avatarUrl: json['avatarUrl'],
      gender: json['gender'] ?? 0,
      birthday: json['birthday'] ?? getTimestamp(),
      region: json['region'] ?? '中国大陆',
      regionCode: json['regionCode'] ?? '86',
      signature: json['signature'] ?? '',
      addType: json['addType'] ?? 1,
      createAt: json['createAt'] ?? getTimestamp(),
    );
  }

  /// 转换为 JSON 结构，用于修改个人信息
  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'phoneNumber': phoneNumber,
      'username': username,
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'gender': gender,
      'birthday': birthday,
      'region': region,
      'regionCode': regionCode,
      'signature': signature,
      'addType': addType,
      'createAt': createAt,
    };
  }

  /// 修改对象信息，并生成新对象
  User copyWith(Map<String, dynamic> updatedFields) {
    return User(
      userId: userId,
      phoneNumber: phoneNumber,
      username: updatedFields['username'] ?? username,
      nickname: updatedFields['nickname'] ?? nickname,
      avatarUrl: updatedFields['avatarUrl'] ?? avatarUrl,
      gender: updatedFields['gender'] ?? gender,
      birthday: updatedFields['birthday'] ?? birthday,
      region: updatedFields['region'] ?? region,
      regionCode: updatedFields['regionCode'] ?? regionCode,
      signature: updatedFields['signature'] ?? signature,
      addType: updatedFields['addType'] ?? addType,
      createAt: createAt,
    );
  }

  //  动态更新部分属性
  Map<String, dynamic> updatePartial(
      Map<String, dynamic> updates, Map<String, dynamic> user) {
    updates.forEach((key, value) {
      if (user.containsKey(key)) {
        user[key] = value;
      }
    });
    return user;
  }
}
